#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "RawsDlg.h"

#define GET_TEXT(id, var) \
	GetDlgItemText(id, buf, 1024); \
	var = buf;

#define ATTACH(id, var) var.Attach(GetDlgItem(id))

LRESULT RawProfileDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	if(currentProfileId != -1) {
		// FIXME disable this for now to stop potential dupes (ahh. fuck it, leave it enabled :p)
		//::EnableWindow(GetDlgItem(IDC_CLIENT_NAME), false);
		adding = false;
		getRawProfile();
	} else {
		adding = true;
		::EnableWindow(GetDlgItem(IDC_NEXT), false);
		::EnableWindow(GetDlgItem(IDC_BACK), false);
	}

	//Sulan start svn212
	int a = IDC_RAW_TEXT1-1;			
	for(int r=1;r<11;++r){
		SetDlgItemText(++a, RAWSETTING(r).c_str());
	}

	//End
	ATTACH(IDC_RAW_PROFILE_NAME, ctrlName);
	ATTACH(IDC_RAW_PROFILE_ONE, ctrlRaw1);
	ATTACH(IDC_RAW_PROFILE_TWO, ctrlRaw2);
	ATTACH(IDC_RAW_PROFILE_THREE, ctrlRaw3);
	ATTACH(IDC_RAW_PROFILE_FOUR, ctrlRaw4);
	ATTACH(IDC_RAW_PROFILE_FIVE, ctrlRaw5);
	ATTACH(IDC_RAW_PROFILE_SIX, ctrlRaw6);
	ATTACH(IDC_RAW_PROFILE_SEVEN, ctrlRaw7);
	ATTACH(IDC_RAW_PROFILE_EIGHT, ctrlRaw8);
	ATTACH(IDC_RAW_PROFILE_NINE, ctrlRaw9);
	ATTACH(IDC_RAW_PROFILE_TEN, ctrlRaw10);

	updateControls();
	
	CenterWindow(GetParent());
	return FALSE;
}

void RawProfileDlg::getRawProfile() {
	ClientProfileManager::getInstance()->getRawProfile(currentProfileId, currentRawProfile);

	name = currentRawProfile.getName();
	raw1 = currentRawProfile.getRaw1();
	raw2 = currentRawProfile.getRaw2();
	raw3 = currentRawProfile.getRaw3();
	raw4 = currentRawProfile.getRaw4();
	raw5 = currentRawProfile.getRaw5();
	raw6 = currentRawProfile.getRaw6();
	raw7 = currentRawProfile.getRaw7();
	raw8 = currentRawProfile.getRaw8();
	raw9 = currentRawProfile.getRaw9();
	raw10 = currentRawProfile.getRaw10();
}

void RawProfileDlg::updateVars() {
	TCHAR buf[1024];

	GET_TEXT(IDC_RAW_PROFILE_NAME, name);
	GET_TEXT(IDC_RAW_PROFILE_ONE, raw1);
	GET_TEXT(IDC_RAW_PROFILE_TWO, raw2);
	GET_TEXT(IDC_RAW_PROFILE_THREE, raw3);
	GET_TEXT(IDC_RAW_PROFILE_FOUR, raw4);
	GET_TEXT(IDC_RAW_PROFILE_FIVE, raw5);
	GET_TEXT(IDC_RAW_PROFILE_SIX, raw6);
	GET_TEXT(IDC_RAW_PROFILE_SEVEN, raw7);
	GET_TEXT(IDC_RAW_PROFILE_EIGHT, raw8);
	GET_TEXT(IDC_RAW_PROFILE_NINE, raw9);
	GET_TEXT(IDC_RAW_PROFILE_TEN, raw10);
}

void RawProfileDlg::updateControls() {
	ctrlName.SetWindowText(name.c_str());
	ctrlRaw1.SetWindowText(raw1.c_str());
	ctrlRaw2.SetWindowText(raw2.c_str());
	ctrlRaw3.SetWindowText(raw3.c_str());
	ctrlRaw4.SetWindowText(raw4.c_str());
	ctrlRaw5.SetWindowText(raw5.c_str());
	ctrlRaw6.SetWindowText(raw6.c_str());
	ctrlRaw7.SetWindowText(raw7.c_str());
	ctrlRaw8.SetWindowText(raw8.c_str());
	ctrlRaw9.SetWindowText(raw9.c_str());
	ctrlRaw10.SetWindowText(raw10.c_str());
}

LRESULT RawProfileDlg::onNext(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {

	updateVars();

	currentRawProfile.setName(name);
	currentRawProfile.setRaw1(raw1);
	currentRawProfile.setRaw2(raw2);
	currentRawProfile.setRaw3(raw3);
	currentRawProfile.setRaw4(raw4);
	currentRawProfile.setRaw5(raw5);
	currentRawProfile.setRaw6(raw6);
	currentRawProfile.setRaw7(raw7);
	currentRawProfile.setRaw8(raw8);
	currentRawProfile.setRaw9(raw9);
	currentRawProfile.setRaw10(raw10);

	ClientProfileManager::getInstance()->updateRawProfile(currentRawProfile);

	dcdebug("RawProfileDlg::onNext1 %s\n", Util::toString(currentProfileId).c_str());

	if(wID == IDC_NEXT && currentProfileId < itemCount - 1)
		currentProfileId++;
	else if(wID == IDC_BACK && currentProfileId > 0)
		currentProfileId--;

	dcdebug("RawProfileDlg::onNext2 %s\n", Util::toString(currentProfileId).c_str());


	getRawProfile();
	updateControls();

	return 0;
}
//End